package fr.upmf.miam.imageannotation;

import fr.upmf.miam.imageannotation.ImageAnnotationContract.People.Picture;
import fr.upmf.miam.imageannotation.ImageAnnotationProvider.C;

import java.util.ArrayList;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class ViewPicture extends Activity  implements OnItemClickListener {
	
	private Uri uri;
	private ArrayList<Integer> listId = new ArrayList<Integer>();

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_view_picture);

		// Initialisation de l'uri
		uri = this.getIntent().getData();
		
		//afficher l'image
		Cursor c = getContentResolver().query(uri, 
			new String[] { ImageAnnotationContract.Picture.URI }, 
			null, null, 
			null
		);

		Uri uriContent = null;
		if(c.moveToFirst()) {
			uriContent = Uri.parse(c.getString(c.getColumnIndex(Picture.URI))); // Uri externe de l'image
		}
		ImageView im = (ImageView)findViewById(R.id.image);
		im.setImageURI(uriContent);
		
		//lister les contacts de l'image
		Cursor contacts = getContentResolver().query(
			Uri.withAppendedPath(uri, "people"), 
			new String[] { ContactsContract.Contacts.DISPLAY_NAME, "people"+C.People._ID }, 
			null, null, 
			null);
		
		ListView listView = (ListView)findViewById(R.id.listContacts);
		String[] listContacts = new String[contacts.getCount()];
		int i = 0;
		while (contacts.moveToNext()) {
		    listContacts[i] = contacts.getString(contacts.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
		    listId.add((int)contacts.getLong(contacts.getColumnIndex("people"+C.People._ID)));
		    i++;
		}
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listContacts);
		listView.setAdapter(adapter);
		
		listView.setOnItemClickListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.view_picture, menu);
		return true;
	}
	
	public void ajouterContact(View view) {
		// Uri de la liste des contacts associés à l'image
		Uri uri = Uri.withAppendedPath(this.getIntent().getData(), "people");
		
		// Intent pour appeler la vue ListContactsActivity
		Intent intent = new Intent();
		intent.setAction(Intent.ACTION_INSERT_OR_EDIT);
		intent.setDataAndType(uri, "image/*");
		startActivity(intent);
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

		// On créé l'uri pour affiche une image
		Uri uri = Uri.withAppendedPath(C.People.CONTENT_URI, listId.get((int) arg3)+"/picture");
		
		// Création de l'intent
		Intent intent = new Intent();
		intent.setAction(Intent.ACTION_VIEW);
		intent.setDataAndType(uri, "contacts/*");
		startActivity(intent);
	}

}
